package com.sto.stoapp.activity.scanner;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SelectTableAdapter;
import com.sto.stoapp.dataprocess.parcelable.GetTableParcel;
import com.sto.stoapp.dataprocess.parcelable.SelectTableParcel;
import com.sto.stoapp.db.manager.SelectTableManager;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.listener.StoOnItemClickListener;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.widget.BaseActivity;

import java.util.List;

public class StoSelectTableActivity extends BaseActivity {
	private TextView backBtn;

	private TextView maxText; // curText

	private ListView courierList;

	private SelectTableAdapter courierAdapter;

	private List<GetTableParcel> courierDatas;

	private SelectTableParcel Entity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.sto_select_table);
		initWidget();
	}

	private void initWidget() {

		backBtn = (TextView) findViewById(R.id.back);

		// curText = (TextView) findViewById(R.id.cur_position);
		maxText = (TextView) findViewById(R.id.total_count);
		// curText.setText(String.valueOf(0));
		courierList = (ListView) findViewById(R.id.courier_list);
		courierList.setOnItemClickListener(itemClickListener);
		initListener();
		Entity = this.getIntent().getParcelableExtra("tableData");
		BufferThread bufferThread = new BufferThread(this);
		bufferThread.start();

	}

	private void initListener() {
		backBtn.setOnClickListener(new StoOnClickListener(this) {
			@Override
			public void mHonClick(View view) {
				finish();
			}
		});
	}

	StoOnItemClickListener itemClickListener = new StoOnItemClickListener(this) {

		@Override
		public void KcOnItemClickListener(AdapterView<?> parent, View view,
				int position, long id) {
			GetTableParcel entity = courierDatas.get(position);
			Intent intent = new Intent();
			intent.putExtra("Name", entity);
			setResult(RESULT_OK, intent);
			finish();
		}
	};

	/**
	 * 查询数据缓冲线程
	 * 
	 * @author Administrator
	 * 
	 */
	class BufferThread extends BaseThread {
		private Context context;

		public BufferThread(Context context) {
			super(context);
			this.context = context;
		}

		@Override
		public void run() {
			SelectTableManager selectTable = new SelectTableManager(context);
			courierDatas = selectTable.queryAll(Entity);
			final int count = courierDatas == null ? 0 : courierDatas.size();

			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (1 == count) {
						GetTableParcel entity = courierDatas.get(count - 1);
						Intent intent = new Intent();
						intent.putExtra("Name", entity);
						setResult(RESULT_OK, intent);
						finish();;

					} else if (0 == count) {
						Intent intent = new Intent();
						setResult(RESULT_FIRST_USER, intent);
						try {
							Thread.sleep(500);
							finish();
						} catch (Exception e) {
							e.printStackTrace();
							ExceptionLog.writeLog("BufferThread",
									e.getMessage());
						}
					} else {
						courierAdapter = new SelectTableAdapter(context,
								courierDatas, Entity);
						courierList.setAdapter(courierAdapter);
						maxText.setText(String.valueOf(count));
					}
				}
			});
		}
	}


}
